package services

import (
	"github.com/pkg/errors"
	"strings"
	"weiboxs.com/new_h_glasses/models"
)

type MssqlService struct {
}

/**
获取HIS用户信息
*/
func (s *MssqlService) GetUserInfo(number string) (*models.UserInfo, error) {
	userInfo := new(models.UserInfo)
	if userlist, err := userInfo.GetOne(number); err != nil {
		return nil, err;
	} else {
		if len(userlist) <= 0 {
			return nil, errors.New("没有找到信息")
		}
		userInfo = userlist[0]
	}
	return userInfo, nil
}

/**
档案数据写入数据表
*/
func (s *MssqlService) GetScreeningInfo(number string, curUser models.BackendUser) (*models.Archives, error) {

	//表已存在，就诊卡记录
	archives, err := models.GetArchivesByNumber(number)
	if (err == nil) {
		return archives, nil
	} else {
		archives = new(models.Archives)
	}

	user, err := s.GetUserInfo(number)
	if err != nil {
		return nil, err
	}

	archives.Mobile = user.Mobile
	archives.Idcard = user.Idcard
	archives.Number = user.PatientIdcard
	if user.Sex == "1" {
		archives.Sex = "男"
	} else {
		archives.Sex = "女"
	}
	if len(user.Birthday) > 0 {
		birthday := strings.Split(user.Birthday, "T")
		user.Birthday = birthday[0]
	}
	archives.Birthday = user.Birthday
	archives.Realname = user.Name
	archives.DepartmentId = curUser.DepartmentId
	if _, err := models.ArchivesAdd(archives); err != nil {
		return nil, err
	}

	return archives, nil
}
